<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试jquery.cookie实现json添加、查询、删除</title>

  <script type="text/javascript" src="jquery-1.4.2.js"></script>
  <script type="text/javascript" src="jquery.cookie.js"></script>
  <script type="text/javascript" src="jquery.jsoncookie.js"></script>
  <script type="text/javascript" src="json2.js"></script>
  <script type="text/javascript">
  	$(function () {
		var cookie_val = $.JSONCookie("bjsx_cookie");//读取json格式cookie

		if (typeof (cookie_val.goods) == "undefined") {//判断cookie是否存在
			cookie_val = { "goods": [{ "goodid": '9527', "img": "abc.jpg", "name": "唐"}]};

			$.JSONCookie("bjsx_cookie", cookie_val, { path: '/', expires: 1 });//存储json格式cookie
		}else {
			for(var i=0; i<cookie_val.goods.length; i++){
				alert(cookie_val.goods[i].goodid);
			}
		}
	});

	function clearhistory() {
		$.cookie("bjsx_cookie", null, { path: '/' });//删除cookie
	}

	function findCookie(){
		var cookie_val = $.JSONCookie("bjsx_cookie");
		if (typeof  cookie_val.goods  != "undefined") {
			for(var i=0; i<cookie_val.goods.length; i++){
					alert(cookie_val.goods[i].goodid);
			}
		}else{
			alert("没有值");
		}
	}
  </script>
 </HEAD>

 <BODY>
  <a href="javascript:findCookie()">查看</a>
  <a href="javascript:clearhistory()">删除</a>
 </BODY>
</HTML>
